package utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;

import model.Model;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class CreateDatabase {
	// JDBC driver name and database URL
	static final String JDBC_DRIVER = "oracle.jdbc.driver.OracleDriver";
	static final String DB_URL = "jdbc:oracle:thin:@localhost:1521:ORCL";

	// Database credentials
	static final String USERNAME = "C##DBUSER";
	static final String PASSWORD = "123456";

	public static void main(String[] args) {
		Connection conn = null;
		Statement stmt = null;
		try {
			Class.forName(JDBC_DRIVER);

			System.out.println("Connecting to database...");
			conn = DriverManager.getConnection(DB_URL, USERNAME, PASSWORD);

			System.out.println("Creating database...");
			stmt = conn.createStatement();

			String sql = "select REMARK from T2";

			ResultSet rs = stmt.executeQuery(sql);
			while (rs.next()) {
				String code = rs.getString("REMARK");
				System.out.println(code);
				Model model = new Model();
				model = JSON.parseObject(code, Model.class);
				model.getName();
				// JSONArray arry = JSONArray.fromObject(code);
				// List<Map<String, String>> rsList = new ArrayList<Map<String, String>>();
				// for (int i = 0; i < arry.size(); i++) {
				// JSONObject jsonObject = arry.getJSONObject(i);
				// Map<String, String> map = new HashMap<String, String>();
				// for (Iterator<?> iter = jsonObject.keys(); iter.hasNext();) {
				// String key = (String) iter.next();
				// String value = jsonObject.get(key).toString();
				// map.put(key, value);
				// System.out.println(key + value);
				// }
				// rsList.add(map);
				// }

			}
		} catch (SQLException se) {
			se.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (stmt != null) {
					stmt.close();
				}
				System.out.println("close");
			} catch (SQLException se2) {
			}
			try {
				if (conn != null) {
					conn.close();
				}
			} catch (SQLException se) {
				se.printStackTrace();
			}
		}
	}
}